package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.core.service.IProductPropService;
import com.wondertek.poms.dao.po.ProductProp;
import com.wondertek.poms.dao.repo.IProductPropDao;
import com.wondertek.poms.dao.vo.ProductPropVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Service
@Scope("prototype")
public class ProductPropServiceImpl extends ServiceImpl<ProductProp,Long> implements IProductPropService {

    @Autowired
    private IProductPropDao productPropDao;
    @PersistenceContext
    public EntityManager entityManager;

    @Override
    public Page<ProductPropVo> findAll(ProductPropVo productProp, int page, int rows, String sord, String sidx) {
        String condition = sqlStringWhere(true,productProp);
        Object o = entityManager
                .createNativeQuery(condition).getSingleResult();
        int count = Convert.toInt(o);
        Pageable pageable = getPageable(page - 1, rows, sord, sidx);
        if(count>0){
            Query query=entityManager.createNativeQuery(sqlStringWhere(false,productProp));
            query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            query.setMaxResults(pageable.getPageSize());
            List<Object[]> resultList=query.getResultList();
            List<ProductPropVo> list = getList(resultList);
            PageImpl<ProductPropVo> result=new PageImpl<>(list,pageable,count);
            entityManager.close();
            return result;
         }
        entityManager.close();
        return Page.empty(pageable);
    }


    public List<ProductPropVo> getList(List<Object[]> resultList){
        List<ProductPropVo> list=new ArrayList<>();
        for(Object[] obj:resultList){
            ProductPropVo pp=new ProductPropVo();
            pp.setId(StringUtil.nullToLong(obj[0]));
            pp.setProductPropName(StringUtil.null2Str(obj[1]));
            pp.setProductPropDesc(StringUtil.null2Str(obj[2]));
            pp.setPropValue(StringUtil.nullToLong(obj[3]));
            pp.setCreateTime(StringUtil.null2Str(obj[4]));
            pp.setUpdateTime(StringUtil.null2Str(obj[5]));
            list.add(pp);
        }
        return list;
    }

    public String sqlStringWhere(boolean getCount,ProductPropVo productProp){
        StringBuilder condition=new StringBuilder();
        condition.append("select ");
        if(getCount){
            condition.append(" count(0) ");
        }else{
            condition.append(" ID,PRODUCT_PROP_NAME,PRODUCT_PROP_DESC,PROP_VALUE," +
                    "to_char(CREATE_TIME,'YYYY-MM-DD HH24:MI:SS'),to_char(UPDATE_TIME,'YYYY-MM-DD HH24:MI:SS') ");
        }
        condition.append( " from PRODUCT_PROP where 1=1 " );
        if(StrUtil.isNotBlank(productProp.getProductPropName())){
            condition.append("and PRODUCT_PROP_NAME like '%"+productProp.getProductPropName()+"%'");
        }
        if(StrUtil.isNotBlank(productProp.getProductPropDesc())){
            condition.append("and PRODUCT_PROP_DESC like '%"+productProp.getProductPropDesc()+"%'");
        }
        return condition.toString();
    }

    @Override
    public ProductProp deleteLogic(Long id) {
         return deleteLogic(id);
    }

    @Override
    public ProductProp merge(ProductProp model) {
        return save(model);
    }
}
